var express = require('express');
var router = express.Router();
let { GoodModel, ShoppcarModel,orderModel } = require("../model/xuweixin")

// 获取商品数据
router.get("/messshow", async (req, res) => {
  let datas = await GoodModel.find()
  res.send({
    code: 200,
    mess: "商品展示成功",
    datas
  })
})

// 获取商品数据
router.post("/addcar", async (req, res) => {
  let newbodys = { ...req.body }

  delete newbodys._id
  // 修改商品售亮
  let good = await GoodModel.findOne({ _id: req.body._id })
  let salesnum = good.salesnum
  await GoodModel.updateOne({ _id: req.body._id }, { $set: { salesnum: salesnum + 1 } })

  // 添加购物车

  let goodone = await ShoppcarModel.find({ $and:[{name: req.body.name},{uid:req.body.uid}]})
  
  if (goodone.length == 0) {
    ShoppcarModel.create(newbodys)
  } else {
    let buynum = await ShoppcarModel.findOne({$and:[{name: req.body.name},{uid:req.body.uid}]})
    await ShoppcarModel.updateOne({$and:[{name: req.body.name},{uid:req.body.uid}]},{ $set: { buynum: buynum.buynum + 1 } })
  }

  res.send({
    code: 200,
    mess: "商品展示成功",
  })
})

//购物车展示
router.get("/carshow", async (req, res) => {
  let datas = await ShoppcarModel.find()
  res.send({
    code: 200,
    mess: "购物车展示数据",
    datas
  })
})

// 修改个数
router.post("/changenum", async (req, res) => {
  let nownum = await ShoppcarModel.findOne({ _id: req.body._id })
  await ShoppcarModel.updateOne({ _id: req.body._id }, { $set: { buynum: req.body.num } })
  res.send({
    code: 200,
    mess: "改变成功"
  })
})

// 移除商品
router.post("/remove", async (req, res) => {
  console.log(req.body)
  await ShoppcarModel.deleteOne({ _id: req.body._id })
  res.send({
    code: 200,
    mess: "删除成功"
  })
})

// 改变商品状态
router.post("/updateflag", async (req, res) => {
  let flag = await ShoppcarModel.findOne({ $and:[{_id: req.body._id},{uid:req.body.uid}]})
  if (flag.flag == true) {
    await ShoppcarModel.updateOne({ $and:[{_id: req.body._id},{uid:req.body.uid}]}, { $set: { flag: false } })
  } else {
    await ShoppcarModel.updateOne({ $and:[{_id: req.body._id},{uid:req.body.uid}]}, { $set: { flag: true } })
  }
  res.send({
    code: 200,
    mess: "修改状态成功"
  })
})

router.post("/changeall",async (req,res)=>{
  console.log(req.body)
  let newdatas=await ShoppcarModel.find()

  newdatas.forEach(item=>{
    req.body.msslis.forEach(items=>{
        if(item._id==items._id){
            item.flag=req.body.flag
        }      
    })
  })

  newdatas.forEach(async item => {
      await ShoppcarModel.updateOne({$and:[{_id: item._id},{uid:req.body.uid}]},{$set:{flag:req.body.flag}})
  });
  let newmess=await ShoppcarModel.find()
  res.send({
    code:200,
    mess:"全部修改",
    newmess
  })
})

router.post("/orderadd",(req,res)=>{
  orderModel.create(req.body.obj)
  res.send({
    code:200,
    mess:"添加订单成功"
  })
})

// 清除购物车
router.post("/delcar", (req,res)=>{
  console.log(req.body.idlis)
  req.body.idlis.forEach(async item=>{
      await ShoppcarModel.deleteOne({_id:item})
  })
  res.send({
    code:200,
    mess:"添加订单成功"
  })
})

// 展示订单详细信息
router.get("/getorder",async (req,res)=>{
  let datas=await orderModel.findOne({order_id:req.query.order_id})
  res.send({
    code:200,
    mess:"订单详细信息",
    datas
  })

})

module.exports = router;
